/**
 * 
 */
package com.imooc.security.core.social;

import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * 继承默认的社交登录配置，加入自定义的后处理逻辑
 * com.imooc.security.core.social.SocialConfig中将此类配置到了容器中，这里不用加@Configuration或者@Compoent
 * 其父类SpringSocialConfigurer会自动把SocialAuthenticationFilter配置到过滤器链中
 */
public class ImoocSpringSocialConfigurer extends SpringSocialConfigurer {
	
	private String filterProcessesUrl;
	// 后处理器，用于区分登录成功后是跳转页面，还是app返回json
	private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;
	
	public ImoocSpringSocialConfigurer(String filterProcessesUrl) {
		this.filterProcessesUrl = filterProcessesUrl;
	}

    /**
  	 * SpringSocialConfigurer：addFilterBefore(postProcess(filter), AbstractPreAuthenticatedProcessingFilter.class);
  	 * 把SocialAuthenticationFilter加入到过滤器链时先执行postProcess方法，此方法可以被覆盖
  	 * @param object
  	 * @param <T>
  	 * @return
  	 */
	@SuppressWarnings("unchecked")
	@Override
	protected <T> T postProcess(T object) {
		SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
        // 给过滤器配置自定义的filterProcessesUrl，默认为:private static final String DEFAULT_FILTER_PROCESSES_URL = "/auth"
        filter.setFilterProcessesUrl(filterProcessesUrl);
		if (socialAuthenticationFilterPostProcessor != null) {
			socialAuthenticationFilterPostProcessor.process(filter);
		}
		return (T) filter;
	}

	/**
	 * filterProcessesUrl
	 *
	 * @return filterProcessesUrl 项目描述（略）
	 */
	public String getFilterProcessesUrl() {
		return filterProcessesUrl;
	}

	/**
	 * filterProcessesUrl
	 *
	 * @param filterProcessesUrl 项目描述（略）
	 */
	public void setFilterProcessesUrl(String filterProcessesUrl) {
		this.filterProcessesUrl = filterProcessesUrl;
	}

	/**
	 * socialAuthenticationFilterPostProcessor
	 *
	 * @return socialAuthenticationFilterPostProcessor 项目描述（略）
	 */
	public SocialAuthenticationFilterPostProcessor getSocialAuthenticationFilterPostProcessor() {
		return socialAuthenticationFilterPostProcessor;
	}

	/**
	 * socialAuthenticationFilterPostProcessor
	 *
	 * @param socialAuthenticationFilterPostProcessor 项目描述（略）
	 */
	public void setSocialAuthenticationFilterPostProcessor(SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor) {
		this.socialAuthenticationFilterPostProcessor = socialAuthenticationFilterPostProcessor;
	}
}
